package com.rp.web.context;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.jfinal.plugin.activerecord.Config;
import com.jfinal.plugin.activerecord.DbKit;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;

/**
 * 项目启动监听器
 * @author 黄健
 */
public class StartListener implements ServletContextListener {
	
	private ApplicationContext appContext;
	
	@Override
	public void contextInitialized(ServletContextEvent event) {
		event.getServletContext().log("启动项目，初始化全局变量...");
		
		//获取Spring上下文
		appContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
		
		DataSource ds = (DataSource) appContext.getBean("dataSource");
		//添加原生数据源Config，原生Config名称为：rpDb
		DbKit.addConfig(new Config("rpDb", ds, new MysqlDialect()));
		
	}
	
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		
	}

}
